package de.tempel.pong.objects.basic;

import java.awt.geom.Point2D;

import processing.core.PApplet;
import de.tempel.pong.objects.interfaces.Drawable;
import de.tempel.pong.objects.interfaces.Moveable;
import de.tempel.pong.util.Line2D;
import de.tempel.pong.util.Rectangle2D;

/**
 * Jedes Spielobjekt ist ein Rechteck.
 * 
 * @author mathias
 * 
 */
public abstract class AbstractGameObject implements Moveable, Drawable {

	/**
	 * Rechteck zum Zeichnen
	 */
	private Rectangle2D rectangle;
	/**
	 * Bewegung des Rechtecks
	 */
	private Vector movement;

	public AbstractGameObject(Point2D position, double width, double heigth,
			Vector movement) {
		super();
		this.rectangle = new Rectangle2D(position.getX(), position.getY(),
				width, heigth);
		this.movement = movement;
	}

	public AbstractGameObject(Point2D position, double width, double heigth) {
		this(position, width, heigth, new Vector(0d, 0d));
	}

	public AbstractGameObject(Point2D position, double size, Vector movement) {
		this(position, size, size, movement);
	}

	public AbstractGameObject(Point2D position, double size) {
		this(position, size, size);
	}

	@Override
	public void draw(PApplet window) {
		window.rect((float) rectangle.getX(), (float) rectangle.getY(),
				(float) rectangle.getWidth(), (float) rectangle.getHeight());
	}

	@Override
	public void move(float tpf) {
		Vector stepDirection = new Vector(movement);
		stepDirection.multiply(tpf);

		Point2D newPosition = getPosition();
		newPosition = stepDirection.movePoint(newPosition);
		this.setPosition(newPosition);
	}

	public boolean touches(Rectangle2D other) {
		return rectangle.intersects(other);
	}

	/**
	 * 
	 * @return Line in die Richtung in die sich das Objekt bewegt
	 */
	public Line2D getMovementLine() {
		Point2D point1 = this.getMiddlePosition();
		Point2D point2 = this.getMovement().movePoint(point1);
		return new Line2D(point1, point2);
	}

	public Point2D getMiddlePosition() {
		return new Point2D.Double(rectangle.getCenterX(),
				rectangle.getCenterY());
	}

	public Point2D getPosition() {
		return new Point2D.Double(rectangle.getX(), rectangle.getY());
	}

	public void setPosition(Point2D position) {
		rectangle = new Rectangle2D(position.getX(), position.getY(),
				rectangle.getWidth(), rectangle.getHeight());

	}

	public double getHeight() {
		return rectangle.getHeight();
	}

	public double getWidth() {
		return rectangle.getWidth();
	}

	public Rectangle2D getRectangle() {
		return rectangle;
	}

	public void setRectangle(Rectangle2D rectangle) {
		this.rectangle = rectangle;
	}

	@Override
	public Vector getMovement() {
		return movement;
	}

	@Override
	public void setMovement(Vector movement) {
		this.movement = movement;
	}

}
